import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:wallet_utils/text_util.dart';

import 'address_select_logic.dart';


class AddressSelectDialog extends StatelessWidget {
  static void show(BuildContext context) {
    showModalBottomSheet(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              topRight: Radius.circular(12.w), topLeft: Radius.circular(12.w)),
        ),
        context: context,
        isScrollControlled: true,
        builder: (context) {
          return AddressSelectDialog();
        });
  }

  final logic = Get.put(AddressSelectLogic());

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height:
      (ScreenUtil().screenHeight * 0.8).w + ScreenUtil().bottomBarHeight,
      child: GetBuilder<AddressSelectLogic>(
        assignId: true,
        builder: (logic) {
          return Column(
            children: [
              //标题
              buildTopBar(),
              Container(
                color: ColorManager.colorF4F4F4,
                height: 0.5.w,
              ),
              //省市区三级已选中的内容
              buildSelectContent(),
              Container(
                color: ColorManager.colorF4F4F4,
                height: 0.5.w,
                padding: EdgeInsets.symmetric(horizontal: 16.w),
              ),
              //地区列表
              buildListWidget(),
            ],
          );
        },
      ),
    );
  }


  SizedBox buildTopBar() {
    return SizedBox(
      height: 48.w,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          ClickWidget(
            onTap: () => Get.back(),
            child: Padding(
              padding: EdgeInsets.symmetric(
                horizontal: 16.w,
                vertical: 13.w,
              ),
              child: Text(
                '取消',
                style: TextStyleUtils.style999999(fontSize: 14.sp),
              ),
            ),
          ),
          Text(
            '公司地址',
            style: TextStyleUtils.style323233(
                fontSize: 16.sp, fontWeight: FontWeight.w600),
          ),
          InkWell(
            onTap: () {
              Get.back();
            },
            child: Padding(
              padding: EdgeInsets.symmetric(
                horizontal: 16.w,
                vertical: 13.w,
              ),
              child: Text(
                '确定',
                style: TextStyleUtils.styleF65247(fontSize: 14.sp),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Container buildSelectContent() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          //省份
          InkWell(
            onTap: () {
              logic.changeAddressType(AddressType.province);
            },
            child: Row(
              children: [
                // Image.asset(
                //   ImagePath.iconAddress,
                //   width: 16.w,
                //   height: 16.w,
                // ),
                Container(
                  margin: EdgeInsets.only(left: 8.w),
                  child: GetBuilder<AddressSelectLogic>(builder: (logic) {
                    return Text(
                      TextUtil.isEmpty(logic.provinceStr)
                          ? '请选择'
                          : logic.provinceStr ?? '',
                      style: TextUtil.isEmpty(logic.provinceStr)
                          ? TextStyleUtils.style999999()
                          : TextStyleUtils.style2A2A2A(),
                    );
                  }),
                )
              ],
            ),
          ),

          //市级
          Visibility(
            visible: !TextUtil.isEmpty(logic.provinceStr),
            child: InkWell(
              onTap: () {
                logic.changeAddressType(AddressType.city);
              },
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 7.5.w, top: 5.w),
                    height: 16.w,
                    width: 1.w,
                    color: ColorManager.colorCCCCCC,
                  ),
                  Row(
                    children: [
                      Container(
                        margin: EdgeInsets.only(left: 5.w),
                        width: 6.w,
                        height: 6.w,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(6.w)),
                          color: ColorManager.color999999,
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 13.w),
                        child: Text(
                          TextUtil.isEmpty(logic.cityStr)
                              ? '请选择市'
                              : logic.cityStr ?? '',
                          style: TextUtil.isEmpty(logic.cityStr)
                              ? TextStyleUtils.styleF65247()
                              : TextStyleUtils.style2A2A2A(),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),

          //区县
          Visibility(
            visible: !TextUtil.isEmpty(logic.provinceStr) &&
                !TextUtil.isEmpty(logic.cityStr),
            child: InkWell(
              onTap: () {
                logic.changeAddressType(AddressType.area);
              },
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 7.5.w, top: 5.w),
                    height: 16.w,
                    width: 1.w,
                    color: ColorManager.colorCCCCCC,
                  ),
                  Row(
                    children: [
                      Container(
                        margin: EdgeInsets.only(left: 5.w),
                        width: 6.w,
                        height: 6.w,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(6.w)),
                          color: ColorManager.color999999,
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 13.w),
                        child: Text(
                          TextUtil.isEmpty(logic.countyStr)
                              ? '请选择区/县'
                              : logic.countyStr ?? '',
                          style: TextUtil.isEmpty(logic.countyStr)
                              ? TextStyleUtils.styleF65247()
                              : TextStyleUtils.style2A2A2A(),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  Expanded buildListWidget() {
    return Expanded(
      child: Padding(
        padding: EdgeInsets.all(16.w),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              logic.addressType == AddressType.province
                  ? '选择省市'
                  : logic.addressType == AddressType.city
                  ? '选择市'
                  : logic.addressType == AddressType.area
                  ? '选择区/县'
                  : '选择省市',
              style: TextStyleUtils.style999999(fontSize: 12.sp),
            ),
            ListView.separated(
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  return InkWell(
                    onTap: () {
                      logic.setAddressStr(logic.addressList[index]);
                    },
                    child: Container(
                      alignment: Alignment.centerLeft,
                      padding: EdgeInsets.symmetric(vertical: 12.w),
                      child: Text(
                        logic.addressList[index],
                        style: TextStyleUtils.style2A2A2A(fontSize: 14.sp),
                      ),
                    ),
                  );
                },
                separatorBuilder: (context, index) {
                  return SizedBox(
                    height: 1.w,
                  );
                },
                itemCount: logic.addressList.length)
          ],
        ),
      ),
    );
  }


}
